/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.deange.ropeprogress;

import com.deange.ropeprogress.provider.ArrayProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * ChooserAbility
 *
 * @author hw
 * @since 2021/5/29
 */
public class ChooserAbility
        extends AbilitySlice implements ListContainer.ItemClickedListener {
    private final List<String> mLabels = new ArrayList<>();
    private final List<Class> mActivities = new ArrayList<>();

    @Override
    protected void onStart(Intent intent) {
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_title_background));
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_chooser);
        mLabels.add(getString(ResourceTable.String_activity_static));
        mLabels.add(getString(ResourceTable.String_activity_dynamic));
        mLabels.add(getString(ResourceTable.String_activity_themed));
        mLabels.add(getString(ResourceTable.String_activity_custom));
        if (findComponentById(ResourceTable.Id_list) instanceof ListContainer) {
            final ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list);
            listContainer.setItemProvider(new TypesAdapter());
            listContainer.setItemClickedListener(this);
        }
    }

    @Override
    public void onItemClicked(ListContainer listContainer, Component component, int position, long lo) {
        if (mLabels.get(position).equals(getString(ResourceTable.String_activity_static))) {
            present(new StaticHeightRopeAbility(), new Intent());
        } else if (mLabels.get(position).equals(getString(ResourceTable.String_activity_dynamic))) {
            present(new DynamicHeightRopeAbility(), new Intent());
        } else if (mLabels.get(position).equals(getString(ResourceTable.String_activity_themed))) {
            present(new ThemedRopeAbility(), new Intent());
        } else if (mLabels.get(position).equals(getString(ResourceTable.String_activity_custom))) {
            present(new CustomTextRopeAbility(), new Intent());
        }
    }

    private class TypesAdapter extends ArrayProvider<String> {
        public TypesAdapter() {
            super(ChooserAbility.this, ResourceTable.Layout_simple_list_item_1, 0, mLabels);
        }
    }
}